package com.shbaiche.bifenba.ui.my;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.cpoopc.scrollablelayoutlib.ScrollableHelper;
import com.google.gson.Gson;
import com.shbaiche.bifenba.App;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.adapter.GirlAdapter;
import com.shbaiche.bifenba.adapter.my.TeamListAdapter;
import com.shbaiche.bifenba.model.GankResults;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.user.FollowCancelMessage;
import com.shbaiche.bifenba.model.user.FollowInfoVo;
import com.shbaiche.bifenba.model.user.FollowUpdateMessage;
import com.shbaiche.bifenba.model.user.MyFollowMessage;
import com.shbaiche.bifenba.model.video.ArticleVo;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.base.BaseScrollAbleFragment;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import org.w3c.dom.Text;

import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroid.base.SimpleRecAdapter;
import cn.droidlover.xdroid.base.XLazyFragment;
import cn.droidlover.xdroidbase.base.XFragmentAdapter;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;
import okhttp3.Call;

/**
 * Created by wanglei on 2016/12/10.
 */
@SuppressLint({"NewApi", "ValidFragment"})
public class FollowTeamFragment extends BaseScrollAbleFragment implements TeamListAdapter.TeamListCallBack {

    @BindView(R.id.contentLayout)
    XRecyclerContentLayout contentLayout;
    @BindView(R.id.more_follow)
    TextView more;

    TeamListAdapter adapter;

    private List<FollowInfoVo> list;

    private int type = 1;

    Dialog dg;


    public FollowTeamFragment() {
        super();
    }

    public FollowTeamFragment(List<FollowInfoVo> list, int type) {
        super();
        this.list = list;
        this.type = type;
    }

    public void setData(List<FollowInfoVo> list) {
        this.list = list;
        adapter.setData(list);
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        dg = new Dialog(getActivity());
        dg.setContentView(R.layout.view_loading);
        initAdapter();
        loadData(1);
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MoreFollowActivity.launch(getActivity());
            }
        });
    }

    private void initAdapter() {
        setLayoutManager(contentLayout.getRecyclerView());
        contentLayout.getRecyclerView()
                .setAdapter(getAdapter());
        contentLayout.getRecyclerView()
                .setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener() {
                    @Override
                    public void onRefresh() {
                        loadData(1);
                    }

                    @Override
                    public void onLoadMore(int page) {
                        loadData(page);
                    }
                });
        contentLayout.getRecyclerView().setRefreshEnabled(false);

        contentLayout.loadingView(View.inflate(getContext(), R.layout.view_loading, null));
//        contentLayout.getRecyclerView().useDefLoadMoreView();
    }

    public void loadData(final int page) {
        getAdapter().setData(list);
    }

    public SimpleRecAdapter getAdapter() {
        if (adapter == null) {
            adapter = new TeamListAdapter(context, this);
        }
        return adapter;
    }

    private void cancelPost(FollowInfoVo item) {
        dg.show();
        FollowCancelMessage msg = new FollowCancelMessage();
        msg.setPassport(App.getPassport());
        msg.setGameType(item.getGameType());
        msg.setFollowId(item.getFollowId());
        if (type == 1) {
            msg.setType(FollowUpdateMessage.FollowType.TEAM);
        } else {
            msg.setType(FollowUpdateMessage.FollowType.COMPETE);
        }
        NetApi.FollowCancel(msg, new JsonCallback<TransMessage>() {

            @Override
            public void onFail(Call call, Exception e, int id) {
                dg.hide();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show("取消失败!" + e.getMessage());
                dg.hide();
            }

            @Override
            public void onResponse(TransMessage response, int id) {
                dg.hide();
                Gson gson = new Gson();
                FollowCancelMessage msg = gson.fromJson(response.getMessage(), FollowCancelMessage.class);

                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, msg.getCode())) {
                    ToastUtil.show(msg.getMessage() + "");
                } else {
                    ToastUtil.show(msg.getMessage() + "");
                }
            }
        });
    }


    public void setLayoutManager(XRecyclerView recyclerView) {
        recyclerView.verticalLayoutManager(context);
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_follow_team;
    }

    @Override
    public View getScrollableView() {
        return contentLayout;
    }

    @Override
    public void cancel(FollowInfoVo item, int pos) {
        cancelPost(item);
        list.remove(pos);

        getAdapter().setData(list);
    }

    @Override
    public void onDestroy() {
        if (dg != null) {
            dg.dismiss();
            dg = null;
        }
        super.onDestroy();
    }
}
